//
//  BlendModeView.m
//  iOSStudy
//
//  Created by Evan Xie on 12/3/15.
//  Copyright © 2015 Family Wings. All rights reserved.
//

#import "BlendModeView.h"


#define kBarNumber 6
#define kBarSize  40

@interface BlendModeView ()
{
    UIColor *_colors[6];
    
    NSArray *_blendModeNames;
    
    UIImage *_blendImage;
}
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
@end

@implementation BlendModeView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        _currentBlendMode = kCGBlendModeNormal;
        
        self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeBlendMode:)];
        self.tapGesture.numberOfTouchesRequired = 1;
        self.tapGesture.numberOfTapsRequired = 2;
        [self addGestureRecognizer:self.tapGesture];
        
        _colors[0] = [UIColor cyanColor];
        _colors[1] = [UIColor magentaColor];
        _colors[2] = [UIColor greenColor];
        _colors[3] = [UIColor yellowColor];
        _colors[4] = [UIColor redColor];
        _colors[5] = [UIColor orangeColor];
        
        _blendImage = [UIImage imageNamed:@"26.jpg"];
        
        _blendModeNames = @[
                            @"kCGBlendModeNormal",
                            @"kCGBlendModeMultiply",
                            @"kCGBlendModeScreen",
                            @"kCGBlendModeOverlay",
                            @"kCGBlendModeDarken",
                            @"kCGBlendModeLighten",
                            @"kCGBlendModeColorDodge",
                            @"kCGBlendModeColorBurn",
                            @"kCGBlendModeSoftLight",
                            @"kCGBlendModeHardLight",
                            @"kCGBlendModeDifference",
                            @"kCGBlendModeExclusion",
                            @"kCGBlendModeHue",
                            @"kCGBlendModeSaturation",
                            @"kCGBlendModeColor",
                            @"kCGBlendModeLuminosity",
                            
                            @"kCGBlendModeClear",                  /* R = 0 */
                            @"kCGBlendModeCopy",                   /* R = S */
                            @"kCGBlendModeSourceIn",               /* R = S*Da */
                            @"kCGBlendModeSourceOut",              /* R = S*(1 - Da) */
                            @"kCGBlendModeSourceAtop",             /* R = S*Da + D*(1 - Sa) */
                            @"kCGBlendModeDestinationOver",        /* R = S*(1 - Da) + D */
                            @"kCGBlendModeDestinationIn",          /* R = D*Sa */
                            @"kCGBlendModeDestinationOut",         /* R = D*(1 - Sa) */
                            @"kCGBlendModeDestinationAtop",        /* R = S*(1 - Da) + D*Sa */
                            @"kCGBlendModeXOR",                    /* R = S*(1 - Da) + D*(1 - Sa) */
                            @"kCGBlendModePlusDarker",             /* R = MAX(0, (1 - D) + (1 - S)) */
                            @"kCGBlendModePlusLighter"];
    }
    return self;
}

- (void)dealloc
{
    [self removeGestureRecognizer:self.tapGesture];
}

- (void)changeBlendMode:(UITapGestureRecognizer *)gesture
{
    ++_currentBlendMode;
    
    if (_currentBlendMode > kCGBlendModePlusLighter) {
        _currentBlendMode = kCGBlendModeNormal;
    }
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);
    
    CGFloat spacing = (rect.size.height - kBarNumber*kBarSize)/2;
    
    // Draw 6 horizontal bars as the background
    CGRect gridFrame;
    for (int index = 0; index < kBarNumber; index++) {
        gridFrame.origin.x = 0;
        gridFrame.origin.y = spacing + index*kBarSize;
        gridFrame.size.width = rect.size.width;
        gridFrame.size.height = kBarSize;
    
        CGContextSetFillColorWithColor(context, _colors[index].CGColor);
        CGContextFillRect(context, gridFrame);
    }
    
    CGContextSaveGState(context);
    
    CGContextSetBlendMode(context, _currentBlendMode);
    
#if 0
    // Draw 6 vertical bars as the forground
    spacing = (rect.size.width - kBarNumber*kBarSize)/2;
    for (int index = 0; index < kBarNumber; index++) {
        gridFrame.origin.x = spacing + index*kBarSize;
        gridFrame.origin.y = 40;
        gridFrame.size.width = kBarSize;
        gridFrame.size.height = rect.size.height;
        
        CGContextSetFillColorWithColor(context, _colors[index].CGColor);
        CGContextFillRect(context, gridFrame);
    }
#else
    
    // Draw an image as the forground
    [_blendImage drawInRect:CGRectMake(0, 0, rect.size.width, rect.size.height) blendMode:_currentBlendMode alpha:1.0];
    
    /** The image will be drawn to upside down. */
    //CGContextDrawImage(context, CGRectMake(20, spacing, rect.size.width-40, rect.size.height-2*spacing), _blendImage.CGImage);
    
#endif
    
    CGContextRestoreGState(context);
    
    // Draw strings.
    [@"Double tap to change the blend mode" drawAtPoint:CGPointMake(10, 0) withAttributes:nil];
    
    NSDictionary *fontAttrs = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0], NSForegroundColorAttributeName:[UIColor blueColor]};
    NSString *blendModeName = _blendModeNames[_currentBlendMode];
    [blendModeName drawAtPoint:CGPointMake(10, 15) withAttributes:fontAttrs];
}

@end
